package com.example.it.review.acvitity.control;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.it.review.R;

public class TextViewActivity extends AppCompatActivity {

    TextView tvWithImage;
    private TextView tvWithSpan;
    private TextView tvSpan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);

        this.tvWithImage = (TextView) this.findViewById(R.id.tvWithImage);
        //获取Drawable数组
        Drawable[] drawables = tvWithImage.getCompoundDrawables();
        drawables[1].setBounds(0, 0, 50, 50);
        tvWithImage.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);

        TextView tvWithHtml = (TextView) this.findViewById(R.id.tvWithHtml);
        String textHtml = "<a href=\"https://www.baidu.com\">点击我访问百度</a>";
        tvWithHtml.setText(Html.fromHtml(textHtml));
        tvWithHtml.setMovementMethod(LinkMovementMethod.getInstance());

        tvSpan = (TextView) this.findViewById(R.id.tvWithSpanBuilder);
        initPraise();
        tvSpan.setMovementMethod(LinkMovementMethod.getInstance());
    }

    void initSpan() {
        SpannableString spannableString = new SpannableString("Hello我是删除线,我能打电话，这是一个图片");
        spannableString.setSpan(new StrikethroughSpan(), 0, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new URLSpan("tel:110"), 13, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 21, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvWithSpan.setText(spannableString);
    }

    /**
     * 实现部分可点击的TextView
     */
    void initPraise() {

        //初始化一些简单的字符串数据
        StringBuffer stringBuffer = new StringBuffer();

        for (int i = 0; i < 20; i++) {
            stringBuffer.append("Friend " + (i + 1) + ',');
        }
        String value = stringBuffer.toString();

        //拼接字符串
        SpannableStringBuilder builder = new SpannableStringBuilder();
        //实例化一个赞的图标
        SpannableString spannableString = new SpannableString("p");
        ImageSpan imgSpan = new ImageSpan(this, R.drawable.praise);
        spannableString.setSpan(imgSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        builder.append(spannableString);
        builder.append(value);
        String[] strArray = value.split(",");

        for (final String item : strArray) {
            int inidex = value.indexOf(item) + 1;
            builder.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(TextViewActivity.this, item, Toast.LENGTH_SHORT).show();
                }
            }, inidex, inidex + item.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        tvSpan.setText(builder);

    }


}
